package com.atguigu.orderservice.controller;


import com.atguigu.common_uitls.R;
import com.atguigu.orderservice.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author wzh
 * @since 2022-10-06
 */
@RestController
@RequestMapping("/eduorder/paylog")
public class PayLogController {
    @Autowired
    private PayLogService payLogService;

    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo){
        //返回一些相关信息,包含二维码的地址和其他信息
        Map map=payLogService.createNative(orderNo);
        return R.ok().data("map",map);
    }
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo){
        Map<String,String> map=payLogService.queryPayStatus(orderNo);
        if (map==null){
            return R.error().message("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")){
            //更新支付状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return null;
    }
}

